﻿#include "stdafx.h"
#include "CourseManageService.h"
#include "TakesDB.h"
#include "CourseDB.h"
#include "StudentDB.h"
/*
这里是课程信息管理系统模块
*/
CourseManageService::CourseManageService()
{
}

list<Course> CourseManageService::lookStuHasCourses(const std::string &sid)
{
	list<string> cIds;//该学生所选课程的所有id
	list<Course> courses;
	TakesDB taDB;
	CourseDB cDB;
	list<Takes> takes = taDB.selectAllByStudentId(sid);
	/*
	int length = takes.size();
	for (int i = 0; i < length; i++) {
		std::string str = takes.front().getCourseId();
		cout << str << endl;
		takes.pop_front();
	}*/
	/*
	list<Takes>::iterator t;
	for (t = takes.begin(); t != takes.end(); t++) {
		(*t).show();
	}
	*/
	
	for each (Takes t in takes)
	{
		cIds.push_back(t.getCourseId());
	}
	for each(string s in cIds)
	{
		courses.push_back(cDB.selectById(s));
	}
	
	return courses;//返回该学生选的所选课程的对象数组
}

void CourseManageService::StuChooseCourse(const std::string &sid, const std::string &cid)
{
	TakesDB taDB;
	Takes t(sid, cid, "");
	taDB.insert(t);
	cout << "选了" << endl;
}

bool CourseManageService::StuReturnCourse(const std::string &sid, const std::string &cid)
{
	TakesDB taDB;
	if (taDB.selectByStudentIdAndCourseId(sid, cid).getGradeId() == "")
	{
		taDB.deleteByStudentIdAndCourseId(sid, cid);
		cout << "退了" << endl;
		return true;
	}
	else
	{
		cout << "已经有成绩，退课失败" << endl;//老师已经给了该学生这门课程的成绩，学生不能退选
		return false;
	}
}

list<Course> CourseManageService::lookMyCourses(const std::string &tid)
{
	CourseDB cDB;
	return cDB.selectAllByTeacherId(tid);
	//如果list为空则该老师没有开过课
}

bool CourseManageService::TeacherOpenCourse(const std::string &cid,const std::string &t,const std::string &tid)
{
	CourseDB cDB;
	list<Course> courses = cDB.selectAll();
	for each(Course c in courses){
		if (cid == c.getCourseId())
		{
			cout << "该课程ID已存在" << endl;
			return false;
		}
	}
	Course course(cid,t,tid);
	cDB.insert(course);
	cout<<"开课成功"<<endl;
	return true;
}

bool CourseManageService::TeacherDeleteCourse(const std::string &tid, const std::string &cid){
	TakesDB taDB;
	CourseDB cDB;
	if (taDB.selectAllByCourseId(cid).size() == 0) {//没有学生选到该门课则可以删除这门课
		cDB.deleteById(cid);
		cout << "删除了"<<cid<< endl;
		return true;
	}
	else {
		cout << "有学生选了该门课，删除失败" << endl;
		return false;
	}

}

list<Student> CourseManageService::lookStuInCourse(const std::string &cid)
{
	TakesDB taDB;
	list<Takes> takes = taDB.selectAllByCourseId(cid);
	list<string> sIds;
	list<Student> students;
	StudentDB sDB;
	for each(Takes t in takes) {
		sIds.push_back(t.getStuId());
	}
	for each(string i in sIds) {
		students.push_back(sDB.selectById(i));
	}
	return students;//返回选修了这门课程的所有学生对象列表
}

void CourseManageService::TeacherDeleteStuFromCourse(const std::string &sid,const std::string &cid)
{
	TakesDB taDB;
	taDB.deleteByStudentIdAndCourseId(sid, cid);
	cout << "删除了"<<sid<<"--"<<cid<< endl;
}


void CourseManageService::TeacherAddStuIntoCourse(const std::string &cid, const std::string &sid)
{
	TakesDB taDB;
	Takes take(sid, cid, "");
	taDB.insert(take);
	cout << "添加了" << sid << "into" << cid << endl;
}

CourseManageService::~CourseManageService()
{
}
